<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录</title>
    <link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
    <link rel="stylesheet" href="./css/common.css" />
    <link rel="stylesheet" href="./css/login.css" />
  </head>
  <body class="application application-offset">
    <div class="container-fluid container-application">
      <div class="main-content position-relative">
        <!-- Page content -->
        <div class="page-content">
          <div class="min-vh-100 py-5 d-flex align-items-center">
            <div class="w-100">
              <div class="row justify-content-center">
                <div class="col-sm-8 col-lg-4">
                  <div class="card shadow zindex-100 mb-0">
                    <div class="card-body px-md-5 py-5">
                      <div class="mb-5">
                        <h6 class="h3">传智教育-注册</h6>
                      </div>
                      <span class="clearfix"></span>
                      <form role="form" autocomplete="off">
                        <div class="form-group">
                          <label class="form-control-label">账号</label>
                          <div class="input-group input-group-merge">
                            <div class="input-group-prepend">
                              <span class="input-group-text"><i class="bi bi-person"></i></span>
                            </div>
                            <input name="username" type="text" class="form-control" id="input-email" placeholder="请输入账号" />
                          </div>
                        </div>
                        <div class="form-group mb-4">
                          <div class="d-flex align-items-center justify-content-between">
                            <div>
                              <label class="form-control-label">密码</label>
                            </div>
                            <div class="mb-2">
                              <a href="javascript:;" class="small text-muted text-underline--dashed border-primary">忘记密码</a>
                            </div>
                          </div>
                          <div class="input-group input-group-merge">
                            <div class="input-group-prepend">
                              <span class="input-group-text"><i class="bi bi-key"></i></span>
                            </div>
                            <input name="password" type="password" class="form-control" id="input-password" placeholder="请输入密码" />
                            <div class="input-group-append">
                              <span class="input-group-text">
                                <a href="javascript:;" data-toggle="password-text" data-target="#input-password">
                                  <i class="bi bi-eye-fill text-blue"></i>
                                </a>
                              </span>
                            </div>
                          </div>
                        </div>
                        <div class="mt-4">
                          <button id="btn-register" type="button" class="btn btn-sm btn-info btn-icon btn-blue rounded-pill">
                            <span class="btn-inner--text">注册</span>
                            <span class="btn-inner--icon"><i class="bi bi-arrow-right"></i></span>
                          </button>
                        </div>
                      </form>
                    </div>
                    <div class="card-footer px-md-5">
                      <small>已经注册？</small> <a href="./login.html" class="small font-weight-bold text-blue">去登录吧</a>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- toast -->
    <div class="position-fixed top-0 start-50 pt-4" style="z-index: 999; transform: translateX(-50%)">
      <div id="myToast" class="toast bg-rgba">
        <div class="toast-body">提示消息</div>
      </div>
    </div>
    <script src="./bootstrap/bootstrap.min.js"></script>
    <script src="./lib/form-serialize.js"></script>
    <script src="./lib/axios.js"></script>
    <script src="./js/common.js"></script>
    <script>
      /*
        1. 找对象
        2. 给按钮注册点击事件
        3. 获取表单的内容 
        3.1 判断表单的值 
        4. 发送请求 - 查看接口文档
        5. 处理成功or失败的业务逻辑
        5.1 成功
        5.1.1 提示用户 - 注册成功
        5.1.2 跳转到登录页面
        5.2. 失败
        5.2.2 提示用户 - 用户名or密码有误...
      */ 
     const btnRegister = document.querySelector('#btn-register')
     const form = document.querySelector('form')
     btnRegister.addEventListener('click',async function() {
      console.log('按钮触发成功了...');
      // 参数二不传： 查询字符串 key=value&ke=value
      // 参数二传递:  js对象 { key:value,key:value }
      // const formData = serialize(form对象, 可选参数)
      const formData = serialize(form, { hash: true })
      // 1. 先提示 2. 再终止函数执行
      // 没有输入内容 or 输入的是空格 => 提示用户
      // ! 取反 包含 false '' 0 null undefined
      if (!formData.username || formData.username.trim() === '') return tip('用户名输入有误..')
      if (!formData.password || formData.password.trim() === '') return tip('密码输入有误..')
      if (formData.password.length < 6) return tip('密码长度不能小于6位..')
      try {
        const res = await axios.post('/register', formData)
        console.log(res);
        // 1. 提示用户
        tip('注册成功!')
        // 2. 跳转到登录页面
        setTimeout(() => {
          location.href = './login.html'
        }, 800);
      } catch (error) {
        // new Error()
        // 以对象的方式打印错误信息 - Error比较特殊
        console.dir(error);
        // http状态码是后端定义的 比如409代表的就是用户名已存在
        if (error.response.status === 409) {
          return tip(error.response.data.message) 
        }
        // 其他情况统一返回服务器异常
        tip('服务器异常, 请稍后再试!')
      }
     })
    </script>
  </body>
</html>
